<?php
class ml_model_dbUserBriefInfo extends Lib_datamodel_db 
{
    public function __construct()
    {
        $db_config = ml_factory::load_standard_conf('db');
        parent::__construct('user_briefinfo' , $db_config['user_briefinfo']);
    }
    
    protected function hook_after_fetch()
    {
        $data = $this->get_data();
        if(isset($data['skill_tags']))
        {
            $data['skill_tags'] = explode(',', $data['skill_tags']);
            $this->set_data($data);
        }else
        {
            foreach($data as $key=>$val)
            {
                if(isset($val['skill_tags']))
                {
                    $val['skill_tags'] = explode(',', $val['skill_tags']);
                }
                $arr[$key]=$val;
            }
            $this->set_data($arr);
        }
        return true;
    }
    
    protected function hook_before_write($array)
    {
        if(isset($array['skill_tags']))
        {
            $array['skill_tags'] = implode(',',$array['skill_tags']);
        }
        return $array;
    }
    
    
    /**
     * 创建记录
     *
     * @param int $uid
     * @return bool
     */
    public function createRecord($uid)
    {
        if(!$this->init_db($uid))
            return false;
        
        if(!$this->fetch_count('`uid` = '.$uid))
            return false;
            
        if($this->get_data() < 1)
        {
            $data = array(
                'uid' => $uid
            );
            return $this->insert($data);
        }
    }

    public function updatePerson($uid , $arrData)
    {
        if(!$this->init_db($uid))
            return false;
            
        $arrUpdate = array(
            'astro'=>$arrData['astro'],
            'access_occupation' => $arrData['access_occupation'],
            'occupation' => $arrData['occupation'],
            'weibo_address' => $arrData['weibo_address'],
            'declaration' => $arrData['declaration']
        );
        return $this->update($arrUpdate , '`uid` = '.$uid);
    }
    
    public function getPersonByUid($uid)
    {
        if(!$this->init_db($uid , self::DB_SLAVE ))
            return false;
            
        $fields = '`astro`,`access_occupation`,`occupation`,`weibo_address`,`declaration`';
        $sql = 'select '.$fields.' from `'.$this->table.'` where `uid` = '.$uid;
        return $this->fetch_row($sql);
    }
    
    /**
     * 修改宣言
     * @gaojian3
     * @param int $uid
     * @param sting $declaration
     * @return unknown 
     */
    public function updateDeclaration($uid,$declaration)
    {
        if(!$this->init_db($uid , self::DB_MASTER ))
            return false;
            
        $arrUpdate['declaration']=$declaration;
        return $this->update($arrUpdate,'`uid` = '.$uid);
    }
    
    /**
     * 获取宣言
     * @gaojian3
     * @param int $uid
     * @return unknown 
     */
    public function getDeclarationByUid($uid)
    {
        if(!$this->init_db($uid , self::DB_SLAVE ))
            return false;
            
        $fields = '`declaration`';
        $sql = 'select '.$fields.' from `'.$this->table.'` where `uid` = '.$uid;
        return $this->fetch_row($sql);
    }
    
    public function getDecAndTagsByUid($uid)
    {
        if(!$this->init_db($uid , self::DB_SLAVE ))
            return false;
            
        $fields = '`declaration` ,`skill_tags` ';
        $sql = 'select '.$fields.' from `'.$this->table.'` where `uid` = '.$uid;
        return $this->fetch_row($sql);
    }
    
    
    
    /**
     * 修改擅长的标签
     * @gaojian3
     * @param unknown_type $uid
     * @param unknown_type $tagArr
     * @return unknown 
     */
    public function updateSkillTags($uid,$tags)
    {
        if(!$this->init_db($uid ))
            return false;
        
        $arrUpdate=array(
                'skill_tags' => $tags
                ); 
        return $this->update($arrUpdate,'`uid` = '.$uid);
    }
    
    /**
     * 获取擅长的标签
     * @gaojian3
     * @param int $uid
     * @return unknown 
     */
    public function getSkillTags($uid)
    {
        if(!$this->init_db($uid , self::DB_SLAVE ))
            return false;
            
        $sql = 'select `skill_tags` from `'.$this->table.'` where `uid` = '.$uid;
        $rs=$this->fetch_row($sql);
        if(!$rs) return false;
        return $rs;
    }
    
    /**
     * 根据uids 获取用户扩展信息
     * @zhanglei11
     * @param array $uids
     * @return bool
     */
    public function getBriefByUids($uids)
    {
        if(!$this->init_db())
            return false;
        if(is_array($uids)) {
            $uids = implode(',', $uids);
            $sql = 'select * from `'.$this->table.'` where `uid` in ( '.$uids.')';
            $rs=$this->query($sql,true);
            if(!$rs) return false;
            return $rs;
        }else return false;
    }
}